<template lang="pug">
div
  ListFilter(
      :showFilter="showFilter"
      :filterData="filterData"
      @close="showFilter = false"
      @confirm="confirm"
      )

  FixedScroll
    div(slot="fixed")
      PageHeader(pageTitle="盘点")
        div(slot="right" @click="showFilter = true") 筛选

      ScanSearch(
          placeholder="盘点通知单号"
          :initSearchValue="searchValue"
          @search="search"
          @scan="search"
          )

    StatTable.table(
        slot="scroll"
        :headList="headList"
        :body="body"
        :isLoading="isLoading"
        @click-row="clickRow"
        )

    Pager(
        slot="fixed-bottom"
        :maxIndex="maxIndex"
        :curIndex="curIndex"
        @jump="jump"
        )

</template>

<script>
import api from "$api"
import util from "$util"

export default {
  data() {
    // 筛选配置
    let filterData = [
      {
        name: "createDate",
        title: "盘点日期",
        tag: "date",
      },
      {
        name: "NotificationNo",
        kTitle: "盘点单号",
        tag: "input",
      },
    ]

    return {
      searchValue: "",
      isLoading: false,
      hasIllegalInput: false,

      loading: {
        confirm: false,
        search: false,
      },

      showFilter: false,
      filterData,

      curIndex: 1,
      maxIndex: 1,
      lastReq: { // 记住上一次请求是搜索还是筛选，跳转页面时要用
        type: "",
        param: null,
      },

      body: {
        list: [],
      },

      headList: [
        { field: "NotificationNo", title: "盘点通知单号", checked: true},
        { field: "Plant", title: "工厂", checked: true},
        { field: "StorgelocationCode", title: "实体仓库", checked: true},
        // { field: "WmNo", title: "逻辑仓库", checked: true},
        // { field: "ZoneNo", title: "存储区", checked: true},
        { field: "ProductionNo", title: "产品线", checked: true},
        { field: "ApplyKeeper", title: "申请人", checked: true},
        { field: "CountTime", title: "计划盘点时间", checked: true},
        { field: "CountStatus", title: "状态", checked: true},
      ],
    }
  },
  methods: {
    async filter(param, pageIndex = 1, pageSize = 20) {
      param.startRowIndex = (pageIndex - 1) * pageSize + 1
      param.maximumRows = pageSize

      this.isLoading = true
      try {
        let data = await api.StockTakingSearch(param)
        if (!data || data.NotificationInfo.length === 0) {
          util.Toast("没有找到符合条件的记录")
          return
        }
        let list = data.NotificationInfo
        this.transform(list)
        this.body.list = list
        this.maxIndex = Math.ceil(data.total / pageSize)
        this.curIndex = pageIndex
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
      }
    },

    // 筛选确定
    async confirm(filter, pageIndex = 1, pageSize = 20) {
      if (this.loading.confirm) {
        return
      }

      let param = {}
      for (let item of filter) {
        switch (item.tag) {
          case "date":
            if (item.value[0]) {
              let start = new Date(item.value[0])
              param.CountTimeStart = `${start.getFullYear()}-${start.getMonth() + 1}-${start.getDate()} 00:00:00`
            }
            if (item.value[1]) {
              let end = new Date(item.value[1])
              param.CountTimeEnd = `${end.getFullYear()}-${end.getMonth() + 1}-${end.getDate()} 23:59:59`
            }
            break
          case "input":
            param[item.key] = item.value[0]
            break
        }
      }

      this.lastReq.type = "confirm"
      this.lastReq.param = filter

      this.loading.confirm = true
      await this.filter(param, pageIndex, pageSize)
      this.loading.confirm = false
    },

    async search(value, pageIndex = 1, pageSize = 20) {
      if (this.loading.search) {
        return
      }
      if (value === "") {
        return
      }

      let param = {
        NotificationNo: value,
      }

      this.lastReq.type = "search"
      this.lastReq.param = value

      this.loading.search = true
      await this.filter(param, pageIndex, pageSize)
      this.loading.search = false
    },

    clickRow(row) {
      this.$router.push({
        path: "/stock-taking/detail",
        query: {
          NotificationNo: row.NotificationNo,
          StorgelocationCode: row.StorgelocationCode,
        },
      })
    },

    transform(list) {
      let enumList = api.Enum.StockTaking.Status.value
      for (let item of list) {
        item.CountStatus = enumList[item.CountStatus]
        if (item.CountTime) {
          let d = new Date(item.CountTime.replace(" ", "T"))
          item.CountTime = d.getUTCFullYear() + "-" + (d.getUTCMonth() + 1) + "-" + d.getUTCDate()
        }
      }
    },

    jump(p) {
      if (this.lastReq.type === "confirm") {
        this.confirm(this.lastReq.param, p.curIndex)
      } else {
        this.search(this.lastReq.param, p.curIndex)
      }
    },

    registerScanner() {
      util.registerScanner((data) => {
        this.searchValue = data
        this.search(data)
      })
    },
  },

  created() {
    this.registerScanner()
  },
}
</script>

<style lang="stylus" scoped>

.info
  border: 1px solid grey200

.table
  scan-table()

.list-item
  border: 1px solid grey500

</style>
